<?php

namespace App\Console\Commands;

use App\Models\Project;
use App\Models\Team;
use App\Models\User;
use App\Models\UserProject;
use App\Models\Volunteer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class SyncUserData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'syncUserData';

    protected $key = 'C3BB66A7552243ED9C7646D07B909408';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '将用户的数据同步到第三方平台';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        Log::info('同步用户数据到第三方平台');

        $p = UserProject::with('user', 'user.volunteer', 'project', 'project.team')->where('sync_time', null)->where('reward_time', '>', 0)->whereHas('user.volunteer', function($query){
            $query->where('id_number', '!=', '');
        })->take(5000)->get();

        foreach($p as $v)
        {
            $tmp = [];
            if(empty($v->user->volunteer->id_number))
            {
                continue;
            }

            $tmp['activity_id'] = strval($v->project->id);
            $tmp['activity_name'] = base64_encode($v->project->name);
            $tmp['activity_unit'] = base64_encode($v->project->team->name);
            $tmp['activity_addr'] = base64_encode($v->project->province_name.$v->project->city_name.$v->project->county_name.$v->project->addr);

            $tmp['activity_min'] = intval($v->reward_time * 60);
            $tmp['activity_begin'] = date("YmdHis", strtotime($v->project->begin));
            $tmp['activity_end'] = date("YmdHis", strtotime($v->project->end));
            $tmp['acitivity_long'] = $v->project->lng;
            $tmp['acitivity_lat'] = $v->project->lat;
            $tmp['user_long'] = '';
            $tmp['user_lat'] = '';
            $tmp['idcard'] = $v->user->volunteer->id_number;
            $tmp['user_name'] = $v->user->volunteer->real_name;

            $list[] = $tmp;

            $v->sync_time = date('Y-m-d H:i:s');

            $v->save();
        }

        if(!is_dir(storage_path('sync')))
        {
            mkdir(storage_path('sync'));
        }
        $file = storage_path('sync/'.date('Y-m-d', time() - 86400).'.json');

        $r = file_put_contents($file, json_encode($list, JSON_UNESCAPED_UNICODE));

        if($r == flase)
        {
            Log::info('同步用户数据存储失败');
        }else{

            //请求对方地址
            $downlink = "http://api.volunteerzhiduoxing.com/api/download/".date('Y-m-d', time() - 86400);
            $url = 'http://yun.cdvolunteer.com/api/open/notify/ServiceFile?key='.$this->key.'&downlink='.urlencode($downlink)."&num=".substr(md5(date('Y-m-d', time() - 86400)), 0 , 30);

            $sync = file_get_contents($url);
            if($sync == false)
            {
                Log::info('同步用户数据失败: '.$url." $sync");
            }else{
                Log::info('同步用户数据成功: '.$url." $sync");
            }
        }



    }
}
